package com.conan.controller;

import com.conan.pojo.Article;
import com.conan.pojo.PageBean;
import com.conan.pojo.Result;
import com.conan.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //新增文章
    @PostMapping
    public Result add(@RequestBody @Validated Article article){
        articleService.addArticle(article);
        return Result.success();
    }

    //文章列表
    @GetMapping
    public Result<PageBean<Article>> list(Integer pageNum,Integer pageSize,
                                      @RequestParam(required = false) String categoryId,
                                      @RequestParam(required = false) String state){
        PageBean<Article> pg = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pg);
    }
    //获取文章详情
    @GetMapping("/detail")
    public Result<Article> getById(@RequestParam Integer id){
        return Result.success(articleService.getById(id));
    }

    //修改文章内容
    @PutMapping
    public Result update(@RequestBody @Validated(Article.Update.class) Article article){
        articleService.update(article);
        return Result.success();
    }

    //删除文章
    @DeleteMapping
    public Result delete(@RequestParam Integer id){
        articleService.delete(id);
        return Result.success();
    }
}
